package com.jianz.campus_markets.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.DeleteById;
import com.baomidou.mybatisplus.core.injector.methods.SelectOne;
import com.jianz.campus_markets.domain.ResponseResult;
import com.jianz.campus_markets.domain.entity.Collection;
import com.jianz.campus_markets.domain.entity.Product;
import com.jianz.campus_markets.mapper.CollectionMapper;
import com.jianz.campus_markets.service.CollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jianz
 * @since 2023-12-28
 */
@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collection> implements CollectionService {

    @Autowired
    private CollectionMapper collectionMapper;

    @Override
    public ResponseResult addCollection(Collection collection) {
        save(collection);
        return ResponseResult.okResult("添加成功");
    }

    @Override
    public ResponseResult getCollection(int userid) {
        List<Product> productList =  collectionMapper.getCollection(userid);
        return ResponseResult.okResult(productList);
    }

    @Override
    public ResponseResult deleteCollection(int userid, int productId) {
        LambdaQueryWrapper<Collection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Collection::getUserId, userid);
        wrapper.eq(Collection::getProductId, productId);
        getBaseMapper().delete(wrapper);
        return ResponseResult.okResult("删除成功");
    }


}
